<?php
/**
 * Created by PhpStorm.
 * Author: huangwy
 * Date: 2018/3/13 14:43
 * Version: 1.0
 */

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\auth\Auth;
use think\facade\Cookie;
use think\Config;
use app\common\model\BaseModel;

class Base extends Controller
{
    public function initialize()
    {
        //config(setting());//保存配置参数,注：thinkphp5 ,批量配置无效,改单一配置
        foreach(setting() as $k => $val){
            config($k,$val);
        }

        $prefix = config('database.prefix');
        if (!config("COOKIE_SALT")){
            $this->error('请配置COOKIE_SALT信息!');
        }

        // 不需要登录控制器 Publics
        if (in_array(request()->controller(), array("Publics"))) {
            $current[0]['title'] = '网站';
            $this->assign('current', $current[0]);
            return true;
        }

        //检测是否登录
        $flag =  $this->check_login();
        $url = url("Publics/login");
        if (!$flag) {
            header("Location: {$url}");
            exit(0);
        }

        //用户组信息
        $model = db('user');
        $UID = $this->USER['uid'];
        $userinfo = $model->query("SELECT * FROM {$prefix}auth_group g left join {$prefix}auth_group_access a on g.id=a.group_id where a.uid=$UID");

        //auth权限,5ini99第三方类库，修改：1、获取配置时config()方法，2、Require方法初始化不需要
        $Auth = new Auth();
        $allow_controller_name = array('Upload');//放行控制器名称
        $allow_action_name = array();//放行函数名称
        if ($userinfo[0]['group_id'] != 1 && !$Auth->check(request()->controller() . '/' . request()->action(),
                $UID) && !in_array(request()->controller(), $allow_controller_name) && !in_array(request()->action(),
                $allow_action_name)){
            $this->error('没有权限访问本页面!');
        }
        //获取用户信息
        $user = member_info(intval($UID),'uid,user,head,sex');
        $this->assign('user', $user);

        $current_action_name = request()->controller() == 'edit' ? "index" : request()->action();
        //select * from A表   A left join B表  B ON B.条件 = A.条件   where A.条件 = 值
        $current = db('user')->query("SELECT s.id,s.title,s.name,s.tips,s.pid,p.pid as ppid,p.title as ptitle FROM {$prefix}auth_rule s left join {$prefix}auth_rule p on p.id=s.pid where s.name='" . request()->controller() . '/' . $current_action_name . "'");
        if($current[0] == null){
            $current[0] = '网页';
        }
        $this->assign('current', $current[0]);

        $menu_access_id = $userinfo[0]['rules'];

        if ($userinfo[0]['group_id'] != 1) {
            $menu_where = "AND id in ($menu_access_id)";
        } else {
            $menu_where = '';
        }
        $menu = db('auth_rule')->field('id,title,pid,name,icon')->where("islink=1 $menu_where ")->order('o ASC')->select();
        $menu = getMenu($menu);
        $this->assign('menu', $menu);
    }

    /**
     * 检查是否登录
     * @return boolean
     */
    public function check_login(){
        $flag = false;
        $salt = config("COOKIE_SALT");
        $ip = get_client_ip();
//        $ua = $_SERVER['HTTP_USER_AGENT'];//浏览器
        $ua = 'huangwy';//浏览器
        $auth = cookie('auth');
        $uid = session('uid');

        if ($uid) {
            $user = db('user')->where(array('uid' => $uid))->find();
            if ($user) {
                if ($auth ==  password($uid.$user['user'].$ip.$ua.$salt)) {
                    $flag = true;
                    $this->USER = $user;
                }
            }
        }
        return $flag;
    }
}